package com.fuli.app.service.Serviceimpl;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.Pagination;
import com.fuli.app.common.SystemContext;
import com.fuli.app.dao.ExerciseProblemsDao;
import com.fuli.app.dto.ExerciseProblemsDTO;
import com.fuli.app.dto.query.ExerciseProblemsQueryDTO;
import com.fuli.app.entity.ExerciseProblems;
import com.fuli.app.service.ExerciseProblemsService;
import com.fuli.app.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class ExerciseProblemsServiceImpl implements ExerciseProblemsService {
    @Autowired
    private ExerciseProblemsDao exerciseProblemsDao;

    @Override
    public ActionResult<String> add(ExerciseProblemsDTO dto) {
        ExerciseProblems po = EntityUtils.copyObjectWithInit(dto, ExerciseProblems.class);
        Integer max = exerciseProblemsDao.getMaxSortIndex(dto.getCurriculumId());
        max = max == null ? 0 : max + 1;
        po.setSortIndex(max);
        exerciseProblemsDao.insert(po);
        return new ActionResult<>(po.getId());
    }

    @Override
    public ActionResult<String> update(ExerciseProblemsDTO dto) {
        ExerciseProblems po = exerciseProblemsDao.get(dto.getId());
        if (po == null)
            return new ActionResult<>(-1, "修改失败，不存在id=" + dto.getId() + "的数据");
        po.setContent(dto.getContent());
        po.setCurriculumId(dto.getCurriculumId());
        po.setName(dto.getName());
        exerciseProblemsDao.update(po);
        return new ActionResult<>(po.getId());
    }

    @Override
    public ActionResult<ExerciseProblemsDTO> get(String id) {
        ExerciseProblems exerciseProblems = exerciseProblemsDao.get(id);
        ExerciseProblemsDTO dtos = EntityUtils.copyObject(exerciseProblems, ExerciseProblemsDTO.class);
        return new ActionResult<>(dtos);
    }

    @Override
    public ActionResult<Pagination<ExerciseProblemsDTO>> listByQueryDTO(ExerciseProblemsQueryDTO queryDTO) {
        Pagination<ExerciseProblemsDTO> pagination = new Pagination<>(queryDTO.getPageNo(), queryDTO.getPageSize());
        int count = exerciseProblemsDao.countByQueryDTO(queryDTO);
        pagination.setCount(count);
        if (count > 0) {
            List<ExerciseProblems> poList = exerciseProblemsDao.listByQueryDTO(queryDTO);
            List<ExerciseProblemsDTO> dtoList = EntityUtils.copyList(poList, ExerciseProblemsDTO.class);
            pagination.setRows(dtoList);
        }
        return new ActionResult<>(pagination);
    }


    @Override
    public ActionResult<String> up(String id) {
        ExerciseProblems po = exerciseProblemsDao.get(id);
        if (po == null)
            return new ActionResult<>(-1, "升序失败，不存在id=" + id + "的数据");
        List<ExerciseProblems> poList = exerciseProblemsDao.listAllByCurriculumId(po.getCurriculumId());
        if (CollectionUtils.isEmpty(poList))
            return new ActionResult<>(-1, "升序失败，不存在同级数据");
        for (int i = 0; i < poList.size(); i++) {
            if (id.equals(poList.get(i).getId()) && i != 0) {
                poList.set(i, poList.get(i - 1));
                poList.set(i - 1, po);
                break;
            }
        }
        //重新设置排序
        for (int i = 0; i < poList.size(); i++) {
            poList.get(i).setSortIndex(i);
        }
        exerciseProblemsDao.batchUpdateSortIndex(poList, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<String> down(String id) {
        ExerciseProblems po = exerciseProblemsDao.get(id);
        if (po == null)
            return new ActionResult<>(-1, "降序失败，不存在id=" + id + "的数据");
        List<ExerciseProblems> poList = exerciseProblemsDao.listAllByCurriculumId(po.getCurriculumId());
        if (CollectionUtils.isEmpty(poList))
            return new ActionResult<>(-1, "降序失败，不存在同级数据");
        for (int i = 0; i < poList.size(); i++) {
            if (id.equals(poList.get(i).getId()) && i != (poList.size() - 1)) {
                poList.set(i, poList.get(i + 1));
                poList.set(i + 1, po);
                break;
            }
        }
        //重新设置排序
        for (int i = 0; i < poList.size(); i++) {
            poList.get(i).setSortIndex(i);
        }
        exerciseProblemsDao.batchUpdateSortIndex(poList, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<String> refresh(String curriculumId) {
        List<ExerciseProblems> poList = exerciseProblemsDao.listAllByCurriculumId(curriculumId);
        if (CollectionUtils.isEmpty(poList))
            return new ActionResult<>(-1, "刷新失败，该课程目录下没有习题");
        //重新设置排序
        for (int i = 0; i < poList.size(); i++) {
            poList.get(i).setSortIndex(i);
        }
        exerciseProblemsDao.batchUpdateSortIndex(poList, SystemContext.getUserId());
        return new ActionResult<>("刷新成功");
    }

    @Override
    public ActionResult<String> delete(String id) {
        ExerciseProblems exerciseProblems = exerciseProblemsDao.get(id);
        if (exerciseProblems == null)
            return new ActionResult<>(id);
        exerciseProblemsDao.delete(id, SystemContext.getUserId());
        return new ActionResult<>(id);
    }

    @Override
    public ActionResult<List<String>> batchDelete(List<String> ids) {
        exerciseProblemsDao.batchDelete(ids, SystemContext.getUserId());
        return new ActionResult<>(ids);
    }

    @Override
    public ActionResult<List<ExerciseProblemsDTO>> getByCurriculumId(String curriculumId) {
        List <ExerciseProblems> exerciseProblems = exerciseProblemsDao.getByCurriculumId(curriculumId);
       List <ExerciseProblemsDTO> dtos = EntityUtils.copyList(exerciseProblems, ExerciseProblemsDTO.class);
        return new ActionResult<>(dtos);
    }
}
